package com.gametime.model;

import com.gametime.dao.PlayerDAO;
import com.gametime.setup.AttributeGenerator;
import com.gametime.setup.ExperienceGenerator;
import com.gametime.setup.HeightGenerator;
import com.gametime.setup.WeightGenerator;

public class Player {

	private int playerId;
	private int experience;
	private String role;
	private Person person;
  private Position position;
  private AttributeSet attributes;
  private SkillSet skills;
  private final String SEPARATOR ="\n";
  
  
  private Player()
  {
    
  }
  
	public String getRole()
  {
    return role;
  }

  public void setRole(String role)
  {
    this.role = role;
  }


  /**
	 * @return the experience
	 */
	public int getExperience() {
		return experience;
	}
	
	
	
	
	public static Player generatePlayerRandomly(Person p, Position pos, String role) throws Exception
	{
		if(p == null)
			throw new Exception("Improper Player initialization, Person is null");
		Player result = new Player();
		
		result.position = pos;
		result.role = role;
		HeightGenerator heightGenerator = HeightGenerator.getInstance();
		WeightGenerator weightGenerator = WeightGenerator.getInstance();
		ExperienceGenerator expGenerator = ExperienceGenerator.getInstance();
		AttributeGenerator attribGenerator = AttributeGenerator.getInstance();
		
		p.setHeight(heightGenerator.getHeight(pos.getPositionId()));
		p.setWeight(weightGenerator.getWeight(p.getHeight()));
		result.person = p;
		result.experience = expGenerator.getExperience(result);
		result.setAttributes(attribGenerator.getAttributes(result));
		return result;
	}
	
	public void setPosition(Position position) 
	{
		this.position = position;
	}
	/**
	 * @param attributes the attributes to set
	 */
	public void setAttributes(AttributeSet attributes) 
	{
		this.attributes = attributes;
	}
	/**
	 * @return the playerId
	 */
	public int getPlayerId() 
	{
		return this.person.getId();
	}
	/**
	 * @return the position
	 */
	public Position getPosition() 
	{
		return position;
	}
	/**
	 * @return the attributes
	 */
	public AttributeSet getAttributes() 
	{
		return attributes;
	}
	/**
	 * @return the traits
	 */
	public SkillSet getSkills() 
	{
		return skills;
	}
	/**
	 * @return the person
	 */
	public Person getPerson() 
	{
		return person;
	}
	/**
	 * @param person the person to set
	 */
	public void setPerson(Person person) 
	{
		this.person = person;
	}
	
	/**
	 * @return the id
	 */
	public int getId() 
	{
		if(isInit())
			return this.person.getId();
		return 0;
	}
	/**
	 * @return the firstName
	 */
	public String getFirstName() 
	{
		if(isInit())
			return this.person.getFirstName();
		return null;
	}
	/**
	 * @return the lastName
	 */
	public String getLastName() 
	{
		if(isInit())
			return this.person.getLastName();
		return null;
	}
	/**
	 * @return the name
	 */
	public String getName() 
	{
		if(isInit())
			return this.person.getName();
		return null;
	}
	
	private boolean isInit()
	{
		if(this.person != null)
			return true;
		else
			return false;
	}
	
	public String toString()
	{
		String out ="<Player playerId ='" + this.playerId + "' experiece='"+ this.experience + "'>" + SEPARATOR;
		if(this.person == null)
			out += "<Person />";
		else
			out += "  " + this.person.toString();
		out += SEPARATOR;
		if(this.position == null)
			out += "<Position />";
		else
			out += "  " + this.position.toString();
		out += SEPARATOR;
		if(this.attributes == null)
			out += "<AttributeSet />";
		else
			out += "  " + this.attributes.toString();
		out += SEPARATOR;
		out += "</Player>";
		return out;
	}
	
	public String formatHeight(int hgt)
	{
		int _a = hgt / 12;
		int _b = hgt % 12;
		return _a + "-"  +_b;
		
	}
	
	public int getWeight()
	{
	  return this.person.getWeight();
	}
	
	public int getRawHeight() {
    // TODO Auto-generated method stub
    return this.person.getHeight();
  }

	public String getHeight() {
		// TODO Auto-generated method stub
		return formatHeight(this.person.getHeight());
	}
	
	public void save() throws Exception
	{
	  PlayerDAO.getInstance().save(this);
	}

  public void saveTeam(int teamId, int seasonId)
  throws Exception
  {
    PlayerDAO.getInstance().saveTeam( this.getPlayerId(), teamId, seasonId);
    
  }
	
	
	

}
